package cn.wolfcode.crm.serivce.impl;

import cn.wolfcode.crm.domain.Member;
import cn.wolfcode.crm.domain.MemberClient;
import cn.wolfcode.crm.domain.RechargeTime;
import cn.wolfcode.crm.mapper.MemberClientMapper;
import cn.wolfcode.crm.mapper.MemberMapper;
import cn.wolfcode.crm.mapper.RechargeTimeMapper;
import cn.wolfcode.crm.query.MemberQueryObject;
import cn.wolfcode.crm.serivce.IMemberService;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class MemberServiceImpl implements IMemberService {
    @Autowired
    private MemberMapper mapper;
    @Autowired
    private MemberClientMapper memberClientMapper;
    @Autowired
    private RechargeTimeMapper rechargeTimeMapper;

    @Override
    public void deleteByPrimaryKey(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(Member record) {
        if (record.getId() == null) {
            //新加一个会员就会去新建一个明细, 存储会员卡号
            MemberClient memberClient = new MemberClient();
            RechargeTime rechargeTime = new RechargeTime();
            memberClient.setCardNumber(record.getCardNumber());
            rechargeTime.setCardNumber(record.getCardNumber());
            //判断为null
            if (memberClient.getAmount() == null){
                memberClient.setAmount(0L);
            }
            if (memberClient.getTotalAmount() == null){
                memberClient.setAmount(0L);
            }
            if (rechargeTime.getAfterTime() == null){
                rechargeTime.setAfterTime(0);
            }
            if (rechargeTime.getBeforeTime() == null){
                rechargeTime.setBeforeTime(0);
            }
            if (rechargeTime.getTime() == null){
                rechargeTime.setTime(0);
            }

            memberClientMapper.insert(memberClient);
            rechargeTimeMapper.insert(rechargeTime);
            record.setMemberClient(memberClient);
            record.setRechargeTime(rechargeTime);
            mapper.insert(record);
            //再把明细和会员两个相关联, 外键
            Long clientId = memberClientMapper.selectByCardNum(record.getCardNumber());
            Long rechargeId = rechargeTimeMapper.selectByCardNum(record.getCardNumber());
            Long memberId=mapper.selectByCardNum(record.getCardNumber());
            //只更新id
            mapper.updateByClientId(clientId,memberId);
            mapper.updateByRechargeId(rechargeId,memberId);
        }else {
            mapper.updateByPrimaryKey(record);
            //如果有更新卡号, 那就把充值和充次中的卡号也更新, 保持一致
            memberClientMapper.updateByCardNumber(record.getCardNumber(), record.getMemberClient().getId());
            rechargeTimeMapper.updateByCardNumber(record.getCardNumber(), record.getRechargeTime().getId());
        }
    }

    @Override
    public Member selectByPrimaryKey(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public PageResult query(MemberQueryObject qo) {

        Integer total = mapper.count(qo);
        if (total > 0) {
            List<Member> rows = mapper.query(qo);

            return new PageResult(total,rows);
        }
        return new PageResult(0, Collections.EMPTY_LIST);
    }

    @Override
    public List<Member> selectAll() {
        return mapper.selectAll();
    }

    @Override
    public Integer selectBirthday() {
        //获取到现在时间, 忽略时分秒
        Date date=new Date();
        java.sql.Date dd=new java.sql.Date(date.getTime());
        return  mapper.selectBirthday(dd);
    }

    @Override
    public Integer selectBirthdayByMonth() {
        //获取到现在当月的时间
        Date date=new Date();
        int month = date.getMonth();
        return  mapper.selectBirthdayByMonth(month+1);
    }

    @Override
    public Integer selectCount() {
        return mapper.selectCount();
    }

    @Override
    public List<Member> selectByChangeKey() {
        return mapper.selectByChangeKey();
    }

    @Override
    public void changeType(Long id) {
         mapper.changeType(id);
    }

    @Override
    public Member selectByMemberClientId(Long id) {
        return mapper.selectByMemberClientId(id);
    }

    @Override
    public Member selectByRechargeTimeId(Long id) {
        return mapper.selectByRechargeTimeId(id);
    }

    @Override
    public Boolean selectByCarNumber(String cardNumber, Long id) {
        Member member = mapper.selectByPrimaryKey(id);
        //如果数据库中有cardNumber, i有值
        int i = mapper.validateName(cardNumber);
        //当前数据库中没有重复或者其id对应的用户名和当前用户名相同,则证明可以
        if (i == 0 || member.getCardNumber().equals(cardNumber)){
            return true;
        }
        return false;//数据库存在相同
    }

    @Override
    public List<Map<String, Object>> orderByAmount() {
        return mapper.orderByAmount();
    }

    @Override
    public BigDecimal selectByTotalAmount(String cardNumber) {
        return mapper.selectByTotalAmount(cardNumber);
    }

    @Override
    public PageResult selectAllByShopName(MemberQueryObject qo) {
        Integer total = mapper.count(qo);
        if (total > 0) {
            List<Member> rows = mapper.selectAllByShopName(qo);

            return new PageResult(total,rows);
        }
        return new PageResult(0, Collections.EMPTY_LIST);
    }

    @Override
    public void zeroPoint(Member member) {
        Member key = mapper.selectByPrimaryKey(member.getId());
        key.setPoints(0);
        mapper.updatePoint(key);
        mapper.updateNumber(key);
    }
}
